package cn.com.inossem.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.util.List;

import butterknife.BindView;
import cn.com.inossem.adapter.TEListCommonAdapter;
import cn.com.inossem.base.RequestFragment;
import cn.com.inossem.http.Api;
import cn.com.inossem.http.NetworkConnectGet;
import cn.com.inossem.model.TeExpenseItemBean;
import cn.com.inossem.model.TeHourItemBean;
import cn.com.inossem.pulltorefresh.widget.XListView;
import cn.com.inossem.utils.TimeUtil;
import cn.com.inossem.utils.ToastUtil;

/**
 * te总工时
 */
public class TeApplyListFragment<T> extends RequestFragment implements XListView.IXListViewListener {
	public static final String KEY_TE = "te";
	public static int TE_STATUS_CHANGE = 1;
	private int type_TE, year, month;
	private TEListCommonAdapter mAdapter;
	private int pageNo;
	@BindView(R.id.te_comm_xlv)
	XListView teCommXlv;
	private JSONObject jb;
	private List<T> mList;
	private Handler mH = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			try {
				jb = new JSONObject((String) msg.obj);
				switch (msg.what) {
					case 1://工时
						mList = new GsonBuilder().create().fromJson(jb.getJSONObject("result").getString("list"), new TypeToken<List<TeHourItemBean>>() {
						}.getType());
						break;
					case 2://报销
						mList = new GsonBuilder().create().fromJson(jb.getJSONObject("result").getString("list"), new TypeToken<List<TeExpenseItemBean>>() {
						}.getType());
						break;
					default:
						return;
				}
				teCommXlv.stopLoadMore();
				teCommXlv.stopRefresh();
				teCommXlv.setPullLoadEnable(mList.size() == 10);
				mAdapter.addData(mList, pageNo);
			} catch (Exception e) {
				e.printStackTrace();
				ToastUtil.show(getActivity(), (String) msg.obj);
			}
		}
	};

	public static TeApplyListFragment newInstance(int type_TE) {
		TeApplyListFragment fragment = new TeApplyListFragment();
		Bundle bundle = new Bundle();
		bundle.putInt(KEY_TE, type_TE);
		fragment.setArguments(bundle);
		return fragment;
	}

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (null != getArguments()) {
			type_TE = getArguments().getInt(KEY_TE);
		}
	}

	@Override
	public int getContentViewId() {
		return R.layout.frag_te_common;
	}

	@Override
	public void initAllMembers(Bundle savedInstanceState) {
		super.initAllMembers(savedInstanceState);
		teCommXlv.setXListViewListener(this);
		teCommXlv.setPullLoadEnable(false);
		teCommXlv.setAdapter(mAdapter = new TEListCommonAdapter(getActivity(), type_TE));
		teCommXlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				startActivityForResult(mAdapter.getApplyNo(position - 1), TE_STATUS_CHANGE);
			}
		});
		onRefresh();
	}

	public void requestData() {
		onClearData();
	}

	public void onClearData() {
		mAdapter.clearData();
		onRefresh();
	}

	@Override
	public void onRefresh() {
		year = MyTEActivity.TE_YEAR;
		month = MyTEActivity.TE_MONTH;
		teCommXlv.setRefreshTime(TimeUtil.getCurrTime());
		pageNo = 1;
		requestTotalHour();
	}

	@Override
	public void onLoadMore() {
		pageNo += 1;
		requestTotalHour();
	}

	private void requestTotalHour() {
		new NetworkConnectGet(getActivity(), Api.pro_ApplyList(type_TE, pageNo) + "&year=" + year + "&month=" + month, mH, type_TE);
	}
}